var Renyuan = new Vue({
	
	el : '#Renyuanlist',
	data :{
		username:'',

		renyuan : {},
		
		renyuanlist : [],
		
		renyuanId:'',
		editFlag : 0,					// 0:新增，1：修改
		show : false,					// 显示列表是否有数据
		
		
		pageIndex : 1,
		pageSize : 10,
		pageCount : 0,
		recordCount : 0,
		inputPageIndexValue : "",
		
		
	},
	created : function() {

		$(".nav navbar-nav li").removeClass("active");		
		$("#nav_renyuan").addClass("active");


		this.findVaild();
	},
	methods : {
		
		findVaild: function(){
			var _this = this;
			layer.open({type:3});
			$.post("/user/findvaild",{
				username :_this.username,
				pageindex : _this.pageIndex,
				pagesize : _this.pageSize,
	    		rdm : Math.random()
	    	},function(ppData){
	    		layer.closeAll("loading");
	    		if (ppData != null){
	    			if(ppData.result == "1"){
	    				var data=ppData.resultContent;
	    				
	    				
	    				if(data.RenYuanList.length > 0){
	    					_this.show = true;
		    				_this.renyuanlist=data.RenYuanList;
		    				
		    				var PageInfo = data.PageInfo;
			    			_this.pageIndex = PageInfo.pageIndex;
			    			_this.recordCount = PageInfo.recordCount;
			    			_this.pageCount = PageInfo.pageCount;
		    				
	    				}else {
	    					_this.show = false;	    	    			
	    				}
	    				
	    			}else{
	    				 layer.alert(ppData.message);						  
					 }
	    		}
	    		
	    	},"json");
		},
		
		
		toAdd : function(){
			
			$("#renyuanModal").modal();
			this.editFlag = 0;
			$("#myModalLabel").html("新增人员");
			this.findVaild();
			this.renyuan.username='';
			this.renyuan.account='';
			this.renyuan.password='';
			this.renyuan.buzhibie='';
			
		},
	
		
		add: function(){
			var _this = this;
			if (!$.trim(_this.renyuan.username)) {
				layer.alert("请填写姓名！");				
				return false;
			}
			
			if (!$.trim(_this.renyuan.account)) {
				layer.alert("请填写账号！");
				return false;
			}
			
			if (!$.trim(_this.renyuan.password)) {
				layer.alert("请填写密码！");
				return false;
			}
			

			
			layer.open({type:3});
			$.post('../../user/add',{
				username:$.trim(_this.renyuan.username),
				account:$.trim(_this.renyuan.account),
				password:$.trim(_this.renyuan.password),
				buzhibie:$.trim(_this.renyuan.buzhibie),
				random : Math.random()
			},function(ppData){
				if(ppData != null){
					layer.closeAll("loading");
					
					if(ppData.result == "1"){
						layer.open({
							time:1000,
							btn:[],
							content:"新增成功!",
						});
						$("#renyuanModal").modal("hide");
						_this.findVaild();
				   }else{
					   layer.alert(ppData.message);
				   }
				}
			},"json");
			
		},
		
		bindRenyuan :function(){
			
			var _this = this;
			layer.open({type:3});
			$.post('/user/find_one', {
				userID : _this.userId,
				rdm : Math.random()
			},function(ppData) {
				layer.closeAll("loading");
				if(ppData != null){
					
					if(ppData.result == "1"){
						var data = ppData.resultContent
						_this.renyuan = data;												
					}else{
						layer.alert(ppData.message);
					}
				}
			},"json");						
		},
				
		
		
		toModify : function(ppUserID){
			$("#renyuanModal").modal();
			this.editFlag = 1;
			$("#myModalLabel").html("修改人员信息");
			
			this.userId=ppUserID;
            this.renyuan = {};			
			this.bindRenyuan();
		},
		
		modify : function(){
			var _this = this;
			if (!$.trim(_this.renyuan.username)) {
				layer.alert("请填写姓名！");				
				return false;
			}
			
			if (!$.trim(_this.renyuan.account)) {
				layer.alert("请填写账号！");
				return false;
			}
			
			if (!$.trim(_this.renyuan.password)) {
				layer.alert("请填写密码！");
				return false;
			}
			
			layer.open({type:3});
			$.post('/user/modify',{
				userID:_this.userId,
				username:$.trim(_this.renyuan.username),
				account:$.trim(_this.renyuan.account),
				password:$.trim(_this.renyuan.password),
				buzhibie:$.trim(_this.renyuan.buzhibie),
				random : Math.random()
			},function(ppData){
				if(ppData != null){
					layer.closeAll("loading");
					
					if(ppData.result == "1"){
						layer.open({
							time:1000,
							btn:[],
							content:"修改成功!",
						});
						$("#renyuanModal").modal("hide");
						_this.findVaild();
				   }else{
					   layer.alert(ppData.message);
				   }
				}
			},"json");
			
			
		},
		
		
		toDelete : function(ppUserID){
			var _this = this;
			
			layer.confirm("确定删除该条人员信息吗？",{
				btn : ['是','否']
			},function(){
				layer.open({type:3});
				
				$.post("/user/delete", {
					userID : ppUserID,
					random : Math.random()
				}, function(ppData) {
					if (ppData != null) {
						layer.closeAll("loading");
						
						if(ppData.result != "1"){
							layer.alert(ppData.message);
						}else{
							layer.open({
								time:1000,
								btn:[],
								content:"删除成功!",
							});
							
							_this.findVaild();
						}
					}
				},"json");
			})								
		},
		
		
		//跳到首页
		SetPageIndex : function(){
			this.pageIndex = 1;
		},
		SetPageEnd : function(){
			this.pageIndex = this.pageCount;
		},
		//上一页
		SetPageIndexPrePage : function(){
			var PrePage = ((this.pageIndex -1) <= 0) ? 1 : (this.pageIndex -1);
			this.pageIndex = PrePage;
		},
		//下一页
		SetPageIndexNextPage : function(){
			var NextPage = ((this.pageIndex +1) >= this.pageCount) ? this.pageCount : (this.pageIndex +1);
			this.pageIndex = NextPage;
		},
		//跳转界面
		JumpPage : function(){
			if(this.inputPageIndexValue <= 1){
				this.inputPageIndexValue = 1;
			}else if(this.inputPageIndexValue >= this.pageCount){
				this.inputPageIndexValue = this.pageCount;
			}
			this.pageIndex = this.inputPageIndexValue;
			this.inputPageIndexValue = '';
		},
		
		
		
	
	},
	watch :{
		//监控分页情况，刷新列表
		pageIndex : function(){
			this.findVaild();
		}
	}
	
});
